#include <cstdlib>
#include <sstream>
#include <string>
#include <iostream>
#include <stdio.h>

#include <fstream>

#include "BigInt.h"
#include "Pvcalc.h"

#define NTRY 5

#define PV_ACCURRACY 6

using namespace std;

int main( )
{

    int size_val[9] = { 50 , 100 , 200 , 300 , 500 , 750 , 1000 , 1250 , 1500 };

    float wantedPvalue = 1.0 ;
    for(int i =0 ; i<PV_ACCURRACY ; i++){ wantedPvalue = wantedPvalue / 10.0 ; }


    //system("del fichier.txt");

    for( int width_i =  8 ; width_i < 9 ; width_i++  )
    {
          std::ofstream file( "fichier.txt", std::ios_base::app );
          
          for( int height_i =  5 ; height_i < 9 ; height_i++  )
          {
               int width  =  size_val[width_i] ;
               int height =  size_val[height_i] ;
               
               cout <<endl<< "width="<<width << "height="<< height <<endl;
               
               float score = 0 ;
               float bottom_score = 0.0 ;
               float top_score    = 1.0 ;
               
               float pvalue = 1.0 ;
               
               short Ntry = 0 ;
               
               float diff = (wantedPvalue - pvalue) ; if( diff < 0 )diff=-diff;
               
               float improv = 1.0;               
               
               cout << "wanted="<<wantedPvalue<<" pv="<<pvalue<<" diff="<<diff<<endl;
                
               bool isInf = false ; 
               while( Ntry < NTRY  || !isInf )
               //while( (  diff > wantedPvalue/10.0 ) && ( improv > 0 ) ) 
               {    
                  
                 improv = score ; ;   
                 //prec_diff = score ;                      
                 score = ( bottom_score +  top_score )  / 2.0 ;
                 
                 improv =  improv - score ; if(improv <0 )improv = -improv ;
                 
                
                 Pvcalc* pvc = new Pvcalc( width , height , score , PV_ACCURRACY+1 , false);
                 string ret = pvc->compute();
                 delete pvc ;
                                 
                 std::istringstream ss( ret );
                 ss >> pvalue ; 
                
                 cout <<"\n\tscore  = " << score ;
                 
                 cout <<"\n\tpvalue = " << pvalue ;
                 cout <<"\n\twanted = " << wantedPvalue ;
                 
                 diff = diff = (wantedPvalue - pvalue) ; if( diff < 0 )diff=-diff;
                 cout <<"\n\tdiff="<<diff ;
                 
                 cout <<"\n\tecart score = " << improv ;
                 
                 if( pvalue > wantedPvalue )
                 {
                     bottom_score = score ;
                     cout << "___ SUP ___" << endl ;
                     isInf = false ;
                 }
                 else
                 {
                     top_score = score  ;
                     cout << "___ INF ___" << endl ;
                     isInf = true ;
                 }
                 
                 cout << "\n\tessai : " << Ntry << "/" << NTRY <<endl;
                                  
                 //system("pause");
                 
                 Ntry++; 

               }
              

 
              file << width << "x" << height<< "\t" << score << "\t" << pvalue << "\t" << Ntry << endl ;

          }
          
          file << "\n" ;  
          file.close();
    }
    
    return EXIT_SUCCESS;
}
